﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace diploma.core.data
{

    /// <summary>
    /// Base dataobject class
    /// </summary>
    public abstract class DataObject : IDataObject, ISerializable
    {
        public override string ToString()
        {
            return textField;
        }
        /// <summary>
        /// Text field for some information for object. It can be description, title, name etc.
        /// </summary>
        protected string textField;

        /// <summary>
        /// Object id
        /// </summary>
        protected long id;

        /// <summary>
        /// Constructor with params
        /// </summary>
        /// <param name="_id">Object id</param>
        /// <param name="_textField">Object text field</param>
        public DataObject(long _id, string _textField)
        {
            textField = _textField;
            id = _id;
        }

        /// <summary>
        /// Empty constructor
        /// </summary>
        public DataObject() { }


        #region IDataObject Members

        public string TextField
        {
            get
            {
                return textField;
            }
        }

        public long Id
        {
            get
            {
                return id;
            }
        }

        public virtual DataObjectTypes Type
        {
            get
            {
                return DataObjectTypes.Unknow;
            }
        }

        public void SetTextField(string _value, bool _innerDataSourceChanges)
        {
            textField = _value;
            if (!_innerDataSourceChanges)
            {
                DataRouter.Instance.Act(DataActions.update, this.Type, this);
            }
        }

        public void SetId(long _value, bool _innerDataSourceChanges)
        {
            id = _value;
            if (!_innerDataSourceChanges)
            {
                DataRouter.Instance.Act(DataActions.update, this.Type, this);
            }
        }

        public void SetTextField(string _value)
        {
            textField = _value;
            DataRouter.Instance.Act(DataActions.update, this.Type, this);
        }

        public void SetId(long _value)
        {
            id = _value;
            DataRouter.Instance.Act(DataActions.update, this.Type, this);
        }


        #endregion

        #region ISerializable Members

        public virtual void FromStream(System.IO.Stream _stream)
        {
            id = ByteSerialization.LongFromBytes(_stream);
            textField = ByteSerialization.StringFromBytes(_stream);
        }

        public virtual void ToStream(System.IO.Stream _stream)
        {
            ByteSerialization.LongToBytes(_stream, id);
            ByteSerialization.StringToBytes(_stream, textField);
        }

        #endregion
    }
}
